package com.example.industry.dao.impl;

import com.example.industry.MySqlConnector;
import com.example.industry.dao.ProjectDao;
import com.example.industry.dao.UserDao;
import com.example.industry.entity.Project;
import com.example.industry.enums.Roles;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Repository
public class ProjectDaoImpl implements ProjectDao {
    // TODO 修改 因为属性chairman_id去掉
    @Resource
    UserDao userDao;
    @Override
    public Project findById(Integer id) {
        Project project = new Project();
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from project where project_id = " + id;
            try (Statement stmt = conn.createStatement()) {
                ResultSet resultSet = stmt.executeQuery(sql);
                if (resultSet.next()) {
                    project.setProjectId(resultSet.getInt("project_id"));
                    project.setProjectName(resultSet.getString("project_name"));
                    project.setProjectIntroduction(resultSet.getString("project_introduction"));
                } else {
                    project = null;
                }
            }
            connector.closeConnection();
            return project;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (findById(id) == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "delete from project where project_id = " + id;
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean update(Project project) {
        if (project == null || project.getProjectName() == null || project.getChairmanId() == null
        || project.getProjectIntroduction() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "update project set project_name = " + project.getProjectName() +
                    ",chairman_id = " + project.getChairmanId() +
                    ",project_introduction = " + project.getProjectIntroduction() +
                    " where project_id = " + project.getProjectId();
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean addProject(Project project) {
        if (project == null || project.getProjectName() == null || project.getChairmanId() == null
                || project.getProjectIntroduction() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "insert into project "+"(project_name,chairman_id,project_introduction)" +
                  "values( " + project.getProjectName() + "," + project.getChairmanId() + "," +
                    project.getProjectIntroduction() + ")";
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {

            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean addMember(Integer projectId, Integer userId, String description, Roles role) {
        if (projectId == null  || userId == null || description == null || role == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            String limit;
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            if (role == Roles.ADMIN) limit = "ADMIN";
            else if (role == Roles.OWNER) limit = "OWNER";
                 else if (role == Roles.COMMON) limit = "COMMON";
                      else return Boolean.FALSE;
            String sql = "insert into user_project "+"(project_id,user_id,description,role)" +
                    "values( " + projectId + "," + userId + "," +
                    description + "," + limit + ")";
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean deleteMember(Integer projectId, Integer userId) {
        if (projectId == null  || userId == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "DELETE from user_project "+ " where project_id = " + projectId + " and user_id = " + userId;
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public ResultSet findMembersByProjectId(Integer projectId) {
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from user_project where project_id = " + projectId;
            try (Statement stmt = conn.createStatement()) {
                ResultSet resultSet = stmt.executeQuery(sql);
                connector.closeConnection();
                return resultSet;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
